<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件
use think\Db;

//字符串格式验证
function check_format($str1, $str2)
{
    // $str1:匹配字符串
    // $str2:正则表达式
    if (preg_match($str2, $str1)) {
        //匹配成功
        return true;
    } else {
        //匹配失败
        return false;
    }
}

//后台生成分页
function page($page, $size, $count, $count1)
{
    $get = Request()->param();
    $url = '?';
    foreach ($get as $k => $v) {
        $url .= $k . '=' . $v . '&';
    }
    $start = ($page - 1) * $size + 1;
    $end = $start + $count1 - 1;
    $page_num = ceil($count / $size);
    $page_str = '<div class="dataTables_info" id="DataTables_Table_0_info" role="status" aria-live="polite">显示 ' . $start . ' 到 ' . $end . ' ，共 ' . $count1 . ' 条</div>
			<div class="dataTables_paginate paging_simple_numbers" id="DataTables_Table_0_paginate">
				<a class="paginate_button previous disabled"  data-dt-idx="0" href="' . $url . 'page=1" tabindex="0" id="DataTables_Table_0_previous">首页</a>';
    if ($page == 1) {
        $page_str .= '<a class="paginate_button previous disabled"  data-dt-idx="0" tabindex="0" id="DataTables_Table_0_previous">上一页</a>';
    } else {
        $last = $page - 1;
        $page_str .= '<a class="paginate_button previous disabled"  data-dt-idx="0" href="' . $url . 'page=' . $last . '" tabindex="0" id="DataTables_Table_0_previous">上一页</a>';
    }
    if ($page_num <= 7) {
        for ($i = 1; $i <= $page_num; $i++) {
            if ($i != $page) {
                $page_str .= '<span><a class="paginate_button disabled" href="' . $url . 'page=' . $i . '" data-dt-idx="2" tabindex="0">' . $i . '</a></span>';
            } else {
                $page_str .= '<span><a class="paginate_button current" href="' . $url . 'page=' . $i . '" data-dt-idx="2" tabindex="0">' . $i . '</a></span>';
            }
        }
    } else {
        if ($page < 4) {
            for ($i = 1; $i <= 7; $i++) {
                if ($i != $page) {
                    $page_str .= '<span><a class="paginate_button disabled" href="' . $url . 'page=' . $i . '" data-dt-idx="2" tabindex="0">' . $i . '</a></span>';
                } else {
                    $page_str .= '<span><a class="paginate_button current" href="' . $url . 'page=' . $i . '" data-dt-idx="2" tabindex="0">' . $i . '</a></span>';
                }
            }
        } else if ($page > $page_num - 3) {
            for ($i = $page_num - 7; $i <= $page_num; $i++) {
                if ($i != $page) {
                    $page_str .= '<span><a class="paginate_button disabled" href="' . $url . 'page=' . $i . '" data-dt-idx="2" tabindex="0">' . $i . '</a></span>';
                } else {
                    $page_str .= '<span><a class="paginate_button current" href="' . $url . 'page=' . $i . '" data-dt-idx="2" tabindex="0">' . $i . '</a></span>';
                }
            }
        } else {
            $start = $page - 3;
            $end = $page + 3;
            for ($i = $start; $i <= $end; $i++) {
                if ($i != $page) {
                    $page_str .= '<span><a class="paginate_button disabled" href="' . $url . 'page=' . $i . '" data-dt-idx="2" tabindex="0">' . $i . '</a></span>';
                } else {
                    $page_str .= '<span><a class="paginate_button current" href="' . $url . 'page=' . $i . '" data-dt-idx="2" tabindex="0">' . $i . '</a></span>';
                }
            }
        }
    }
    if ($page == $page_num) {
        $page_str .= '<a class="paginate_button next disabled"  data-dt-idx="2" tabindex="0" id="DataTables_Table_0_next">下一页</a>';
    } else {
        $next = $page + 1;
        $page_str .= '<a class="paginate_button next disabled"  data-dt-idx="2" href="' . $url . 'page=' . $next . '" tabindex="0" id="DataTables_Table_0_next">下一页</a>';
    }
    $page_str .= '<a class="paginate_button next disabled" href="' . $url . 'page=' . $page_num . '"  data-dt-idx="2" tabindex="0" id="DataTables_Table_0_next">最后一页</a>
			</div>';
    return $page_str;
}

//后台返回json数据，记录用户操作
function return_json($data,$permissions_name='')
{
    $get = Request()->param();
    foreach ($get as $k => $v) {
        if (is_string($v)) {
            $get[$k] = urlencode($v);
        }
    }
    $data1 = array();
    foreach ($data as $k => $v) {
        $data1[$k] = urlencode($v);
    }
    $way = Request()->module() . '/' . Request()->controller() . '/' . Request()->action();
    /*
	//权限不涉及三级功能权限，不需要从数据库中读取
	$permissions = db('menu')->where(array('m_url' => $way, 'm_status' => 1))->find();
    if (empty($permissions)) {
        $permissions = array('m_name' => '非权限管理操作');
    }*/
    db('admin_log')->insert(array('way' => $way, 'time' => date('Y-m-d H:i:s'), 'action' => $permissions_name, 'post' => urldecode(json_encode($get)), 'return_json' => urldecode(json_encode($data1)), 'return_code' => $data['code'], 'return_msg' => $data['msg'], 'user_id' => session('uid'), 'user_name' => session('user.real_name'), 'user_ip' => $_SERVER['REMOTE_ADDR']));
    return json($data);
    die;
}
//后台返回json数据，记录用户操作
function form_json($data,$permissions_name='')
{
    $get = Request()->param();
    foreach ($get as $k => $v) {
        if (is_string($v)) {
            $get[$k] = urlencode($v);
        }
    }
    $data1 = array();
    foreach ($data as $k => $v) {
        $data1[$k] = urlencode($v);
    }
    $way = Request()->module() . '/' . Request()->controller() . '/' . Request()->action();
	/*
	//权限不涉及三级功能权限，不需要从数据库中读取
    $permissions = Db::name('permissions')->where(array('url' => $way, 'is_enable' => 1))->find();
    if (empty($permissions)) {
        $permissions = array('name' => '非权限管理操作');
    }*/
    Db::name('admin_log')->insert(array('way' => $way, 'time' => date('Y-m-d H:i:s'), 'action' => $permissions_name, 'post' => urldecode(json_encode($get)), 'return_json' => urldecode(json_encode($data1)), 'return_code' => $data['code'], 'return_msg' => $data['msg'], 'user_id' => session('uid'), 'user_name' => session('user.real_name'), 'user_ip' => $_SERVER['REMOTE_ADDR']));
}
//导出数据excel表
function createtable($list,$header=array(),$filename){
    header("Content-type:application/vnd.ms-excel");
    header("Content-Disposition:filename=".$filename.".xls");
    $teble_header = implode("\t",$header);
    $strexport = $teble_header."\r";
    foreach ($list as $row){
        foreach($row as $v){
            $strexport.=$v."\t";
        }
        $strexport.="\r";

    }
    $strexport=iconv('UTF-8',"GB2312//IGNORE",$strexport);
    exit($strexport);
}
/**
 * 导出数据到CSV文件
 * @param array $data  数据
 * @param array $title_arr 标题
 * @param string $file_name CSV文件名
 */
function export_csv(&$data, $title_arr, $file_name = '') {
    ini_set("max_execution_time", "3600");
    $csv_data = '';
    /** 标题 */
    $nums = count($title_arr);
    for ($i = 0; $i < $nums - 1; ++$i) {
        $csv_data .= '"' . $title_arr[$i] . '",';
    }
    if ($nums > 0) {
        $csv_data .= '"' . $title_arr[$nums - 1] . "\"\r\n";
    }
    foreach ($data as $k => $row) {
        for ($i = 0; $i < $nums - 1; ++$i) {
            $row[$i] = str_replace("\"", "\"\"", $row[$i]);
            $csv_data .= '"' . $row[$i] . '",';
        }
        $csv_data .= '"' . $row[$nums - 1] . "\"\r\n";
        unset($data[$k]);
    }
    $csv_data = mb_convert_encoding($csv_data, "cp936", "UTF-8");
    $file_name = empty($file_name) ? date('Y-m-d-H-i-s', time()) : $file_name;
    if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE")) { // 解决IE浏览器输出中文名乱码的bug
        $file_name = urlencode($file_name);
        $file_name = str_replace('+', '%20', $file_name);
    }
    $file_name = $file_name . '.csv';
    header("Content-type:text/csv;");
    header("Content-Disposition:attachment;filename=" . $file_name);
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
    header('Expires:0');
    header('Pragma:public');
    echo $csv_data;
}


